package com.controller;


import com.bean.Answer;
import com.bean.KillList;
import com.bean.KillNode;
import com.bean.Users;
import com.bean.request.AnswerRequest;
import com.bean.vo.KillListVo;
import com.bean.vo.UserVo;
import com.service.AnswerService;
import com.service.KillListService;
import com.service.KillNodeService;
import com.service.UsersService;
import com.utils.AjaxResult;
import com.utils.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@org.springframework.stereotype.Controller
@RequestMapping("/killNode")
@CrossOrigin
public class Controller {

    @Autowired
    private UsersService usersService;

    @Autowired
    private AnswerService answerService;

    @Autowired
    private KillNodeService killNodeService;

    @Autowired
    private KillListService killListService;



    @RequestMapping("/unauth")
    public AjaxResult unauth() {
        return new AjaxResult(300, "未授权");
    }
//    unauth
    //登录是也，现弄最简单的
    @RequestMapping("/login")
    public AjaxResult login(@RequestBody Users u) {

//        //得到subject
//        Subject subject= SecurityUtils.getSubject();
//        UsernamePasswordToken token = new UsernamePasswordToken(u.getUserName(),u.getPassword());
//        //用login方法登录之
//        subject.login(token);
//        //用subject
//        UserVo a= (UserVo) subject.getPrincipal();
//        UserVo a = usersService.checkOne(u);
        UserVo a = usersService.checkOne(u);
        if (a == null) {
            return new AjaxResult(300, "用户不存在");
        } else {
            return new AjaxResult(200, "登录成功", a);
        }
    }


    //注册是也
    @RequestMapping("/registry")
    public AjaxResult registry(@RequestBody Users u) {
        UserVo a = usersService.checkOne(u);
        if (StringUtils.isNull(a)) {
            int i = usersService.insert(u);
            if (i > 0) {
                return new AjaxResult(200, "注册成功", u);
            } else {
                return new AjaxResult(400, "添加失败系统异常");
            }
        } else {
            return new AjaxResult(300, "用户已存在");
        }
    }


    //查看所有题目
    @RequestMapping("/checkQuestion")
    public AjaxResult checkQuestion() {
        List<Answer> al = answerService.checkAll();

        if (StringUtils.isEmpty(al)) {

            return new AjaxResult(400, "查询问题异常");

        } else {
            Answer answer=al.get(0);
            return new AjaxResult(200, "查询题目成功", answer);
        }
    }


    //答题
    @RequestMapping("/answerQuestion")
    public AjaxResult answerQuestion(@RequestBody AnswerRequest answerRequest) {
        //计算得分

        Integer sum = answerRequest.getAnswer1Score() + answerRequest.getAnswer2Score() +
                answerRequest.getAnswer3Score() + answerRequest.getAnswer4Score();
        Users u = new Users();
        u.setId(answerRequest.getUserId());

        UserVo user = usersService.checkOne(u);
        user.setScore(sum);
        //增加答题记录
        KillNode killNode = new KillNode();
        killNode.setPersonId(answerRequest.getUserId());
        killNode.setScore(sum);
        killNode.setAnswerDate(answerRequest.getDate());

        killNodeService.insert(killNode);

        //给那个人赋予分数


        //给那个人赋予命运
        if (sum < 40) {
            user.setFate("必死");
        } else if (30 < sum && sum < 60) {
            user.setFate("生死待定");
        } else if (60 < sum && sum < 90) {
            user.setFate("一般人");

        } else if (sum >= 90) {
            user.setFate("自己人");
        }
        Users uu=new Users();
        BeanUtils.copyProperties(user,uu);
      usersService.update(uu);
        KillList killList = new KillList();
        //根据得分判断是否加入必杀名单
        if (sum < 40) {
            killList.setKillid(answerRequest.getUserId());
            if (answerRequest.getAnswer2Score() == 0 || answerRequest.getAnswer1Score() == 0) {
                killList.setKillname("苗轮功");
            } else if (answerRequest.getAnswer3Score() == 0 || answerRequest.getAnswer4Score() == 0) {
                killList.setKillname("鞑子蛆");
            }

            killListService.insert(killList);
        }


        return new AjaxResult(200, "答题完毕",user);
    }


    //查看必杀名单是也
    @RequestMapping("/chcekList")
    public AjaxResult chcekList(@RequestBody Users u) {
      List<KillListVo>killListVos=  killListService.checkAll();

      if(StringUtils.isNotEmpty(killListVos)){
          return  new AjaxResult(200,"查看成功",killListVos);
      }else{
          return  new AjaxResult(300,"必杀名单为空",killListVos);
      }
    }

    //查看个人信息是也
    @RequestMapping("/checkOne")
    public AjaxResult checkOne(@RequestBody Users u) {
        UserVo a = usersService.checkOne(u);
        if (StringUtils.isNull(a)) {

                return new AjaxResult(400, "添加失败系统异常");

        } else {
            return new AjaxResult(200, "用户在此",a);
        }
    }

}


